'use strict';
const queryString = require('querystring');
const Controller = require('egg').Controller;

class SpeedController extends Controller {
  async index() {
    const { ctx } = this;
    await ctx.render('speed');
  }

  async run() {
    const rsurl = 'https://www.googleapis.com/pagespeedonline/v4/runPagespeed';

    const { ctx } = this;
    let { url } = ctx.request.body;
    url = queryString.unescape(url);
    console.log('url url', url);
    const body = await ctx.curl(rsurl, {
      dataType: 'json',
      // timeout: 3000,
      data: {
        url,
        locale: 'zh_CN',
      },
    });

    ctx.result({ body });
  }
}

module.exports = SpeedController;
